package com.example.java_chatroom.Controller;

import com.example.java_chatroom.model.Message;
import com.example.java_chatroom.model.Result;
import com.example.java_chatroom.service.MessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collection;
import java.util.Collections;
import java.util.List;

@Slf4j
@RequestMapping("/message")
@RestController
public class MessageController {

    @Autowired
    private MessageService messageService;

    @RequestMapping("/getMessage")
    public Result getMessage(Integer sessionId) {
        List<Message> list = messageService.getMessagesBySessionId(sessionId);
        // 针对查询结果, 进行逆置操作. 毕竟界面上需要的是按照时间升序排列的消息. 此处得到的是降序排列的消息.
        Collections.reverse(list);
        return Result.success(list);
    }
}
